const mixin = {
  data() {
    return {
      defaultGetList: true,
      advanced: false,
      queryParam: {},
      loading: false,
      pagination: {
        position: 'bottom',
        showSizeChanger: true,
        showQuickJumper: true,
        hideOnSinglePage: false,
        current: 1,
        pageSizeOptions: ['5', '10', '15', '20'],
        total: 0,
        pageSize: 10,
        showTotal (total) {
          return `共 ${total} 条`
        },
        onChange: this.onChange,
        onShowSizeChange: this.onShowSizeChange
      },
      dataSet: [],
      columns: []
    }
  },
  beforeMount: function() {
    //// 是否调用 getList 获取数据
    if(this.defaultGetList) {
      this.getList(Object.assign(this.queryParam, { page: 1, page_size: this.pagination.pageSize  }))
    }
  },
  methods: {
    /*
     * 混入之后必须要调用
     * 获取数据函数，接受 请求函数, 请求参数, 返回 promise 对象
     */
    async listHandler(list, params={}) {
      this.loading = true
      try {
        const res  = await list(params)
        this.dataSet = res.data
        this.pagination.total = res.extra.total_items
        this.loading = false
        return Promise.resolve(res)
      } catch (e) {
        this.loading = false
        return Promise.reject(e)
      }

    },
    // 搜索栏 更多按钮显示隐藏内容
    toggleAdvanced () {
      this.advanced = !this.advanced
    },
    // 页数改变
    onChange (page, pageSize) {
      this.pagination.current = page
      this.pagination.pageSize = pageSize
      this.getList({...this.queryParam, page: page, page_size: pageSize })
    },
    // 分页数量改变
    onShowSizeChange (page, pageSize) {
      this.pagination.current = page
      this.pagination.pageSize = pageSize
      this.getList({...this.queryParam, page: page, page_size: pageSize })
    },
    // 搜索栏搜索
    search() {
      this.pagination.current = 1
      this.getList({...this.queryParam, page: 1, page_size: this.pagination.pageSize })
    },
    // 搜索栏重置
    reset() {
      this.pagination.current = 1
      this.queryParam = {}
      this.getList({...this.queryParam, page: 1, page_size: this.pagination.pageSize })
    }
  }
}

export default mixin
